package com.leo.erp.web.plugin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gitee.starblues.integration.application.PluginApplication;
import com.gitee.starblues.integration.operator.PluginOperator;
import com.gitee.starblues.integration.operator.module.PluginInfo;

import com.leo.erp.util.HttpServletRequestUtil;
import com.leo.erp.util.StringUtil;

/**
 * 插件jar 包测试功能
 * @author leo
 *
 */
@Controller
@RequestMapping(value = "/plugin")
public class PluginManagementController {
	private final PluginOperator pluginOperator;
	
	@Autowired
	public PluginManagementController(PluginApplication pluginApplication) {
		this.pluginOperator = pluginApplication.getPluginOperator();
	}
	
	/**
     * 获取插件信息
     * @return 返回插件信息
     */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> getPluginInfo(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String name = HttpServletRequestUtil.getString(request, "name");
		Integer currentPage = HttpServletRequestUtil.getInt(request, "currentPage");
		Integer pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		try {
			List<PluginInfo> resList = new ArrayList<>();
			List<PluginInfo> list = pluginOperator.getPluginInfo();
			if(StringUtil.isEmpty(name)) {
                resList = list;
            } else {
                for(PluginInfo pi : list) {
                    String desc = pi.getPluginDescriptor().getPluginDescription();
                    if(desc.contains(name)) {
                        resList.add(pi);
                    }
                }
            }
			modelMap.put("success", true);
			modelMap.put("rows", resList);
			modelMap.put("total", resList.size());
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}
		
		return modelMap;
	}
	
	/**
     * 根据插件id启动插件
     * @param id 插件id
     * @return 返回操作结果
     */
	@RequestMapping(value = "/start/{id}")
	@ResponseBody
	public Map<String, Object> start(@PathVariable("id") String id){
		Map<String, Object> modelMap = new HashMap<>();
		
		String message = "";
		try {
			if(pluginOperator.start(id)) {
				message = "plugin '" + id +"' start success";
			}else {
				message = "plugin '" + id +"' start failure";
			}
			modelMap.put("success", true);
			modelMap.put("message", message);
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("message", "plugin '" + id +"' start failure. " + e.getMessage());
		}
		
		return modelMap;
	}
	
	/**
     * 上传并安装插件。注意: 该操作只适用于生产环境
     * @param multipartFile 上传文件 multipartFile
     * @return 操作结果
     */
	@RequestMapping(value = "/uploadInstallPluginJar")
	@ResponseBody
	public String install(@RequestParam("jarFile") MultipartFile multipartFile){
        try {
            if(pluginOperator.uploadPluginAndStart(multipartFile)){
                return "install success";
            } else {
                return "install failure";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "install failure : " + e.getMessage();
        }
    }
	
	/**
     * 根据插件id卸载插件
     * @param id 插件id
     * @return 返回操作结果
     */
	@RequestMapping(value = "/uninstall/{id}")
	@ResponseBody
	public Map<String, Object> uninstall(@PathVariable("id") String id){
		Map<String, Object> modelMap = new HashMap<>();
		
		String message = "";
		
		try {
			if(pluginOperator.uninstall(id, true)) {
				message = "plugin '" + id +"' uninstall success";
			}else {
				message = "plugin '" + id +"' uninstall failure";
			}
			modelMap.put("success", true);
			modelMap.put("message", message);
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("message", "plugin '" + id +"' uninstall failure. " + e.getMessage());
		}
		return modelMap;
	}
	
	/**
     * 根据插件id停止插件
     * @param id 插件id
     * @return 返回操作结果
     */
	@RequestMapping(value = "/stop/{id}")
	@ResponseBody
	public Map<String, Object> stop(@PathVariable("id") String id){
		Map<String, Object> modelMap = new HashMap<>();
		
		String message = "";
		try {
			if(pluginOperator.stop(id)) {
				message = "plugin '" + id +"' stop success";
			}else {
				message = "plugin '" + id +"' stop failure";
			}
			modelMap.put("success", true);
			modelMap.put("message", message);
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("message", "plugin '" + id +"' stop failure. " + e.getMessage());
		}
		
		return modelMap;
	}
	
}
